/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "gpu_counter_object_table.h"
#include <cmath>

namespace SysTuning {
namespace TraceStreamer {
enum class Index : int32_t { COUNTER_ID = 0, COUNTER_NAME = 1 };
GpuCounterObjectTable::GpuCounterObjectTable(const TraceDataCache *dataCache) : DemoTableBase(dataCache)
{
    demoTableColumn_.push_back(DemoTableBase::ColumnInfo("counter_id", "INTEGER"));
    demoTableColumn_.push_back(DemoTableBase::ColumnInfo("counter_name", "REAL"));
    demoTablePriKey_.push_back("counter_id");
}

GpuCounterObjectTable::~GpuCounterObjectTable() {}

std::unique_ptr<DemoTableBase::Cursor> GpuCounterObjectTable::CreateCursor()
{
    return std::make_unique<Cursor>(demoTraceDataCache_, this);
}

GpuCounterObjectTable::Cursor::Cursor(const TraceDataCache *dataCache, DemoTableBase *table)
    : DemoTableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstGpuCounterObjectData().Size())),
      gpuCounterObjectDataObj_(dataCache->GetConstGpuCounterObjectData())
{
}

GpuCounterObjectTable::Cursor::~Cursor() {}

int32_t GpuCounterObjectTable::Cursor::Column(int32_t gpuCntObjColumn) const
{
    switch (static_cast<Index>(gpuCntObjColumn)) {
        case Index::COUNTER_ID: {
            sqlite3_result_int64(demoContext_,
                                 static_cast<int64_t>(gpuCounterObjectDataObj_.CounterId()[CurrentRow()]));
            break;
        }
        case Index::COUNTER_NAME: {
            sqlite3_result_text(demoContext_, gpuCounterObjectDataObj_.CounterName()[CurrentRow()].c_str(),
                                STR_DEFAULT_LEN, nullptr);
            break;
        }
        default:
            TS_LOGF("Unregistered gpuCntObjColumn : %d", gpuCntObjColumn);
            break;
    }
    return SQLITE_OK;
}
} // namespace TraceStreamer
} // namespace SysTuning
